package com.zhs.xyzsdk.editors.token;

import java.util.List;

import com.zhs.javacc.XYZParserConstants;

public class TokenList {
	private List<CommonToken> tokenList;
	private boolean dirty;

	/**
	 * Get token by character offset
	 */
	public CommonToken getToken(int offset) {
		if (tokenList == null || tokenList.isEmpty())
			return CommonToken.EOF_TOKEN;
		else if (offset < 0)
			return tokenList.get(0);
		else if (offset >= getLength())
			return CommonToken.EOF_TOKEN;
		int i = 0;
		int j = tokenList.size() - 1;
		do {
			int mid = (i + j) / 2;
			CommonToken token = tokenList.get(mid);
			if (offset >= token.getStartIndex()
					&& offset <= token.getStopIndex())
				return token;
			if (offset < token.getStartIndex()) {
				j = mid - 1;
			} else {
				i = mid + 1;
			}
		} while (i <= j);
		return null;
	}

	/**
	 * Get next token of given token, or null if this is last token
	 */
	public CommonToken getNextToken(CommonToken refToken) {
		if (tokenList == null || refToken == null)
			return null;
		else {
			int index = tokenList.indexOf(refToken);
			if (index == tokenList.size() - 1) {
				if (refToken.getToken().kind == XYZParserConstants.EOF)
					return null;
				else
					return CommonToken.EOF_TOKEN;
			} else
				return tokenList.get(index + 1);
		}
	}

	/**
	 * Get source code length
	 */
	public int getLength() {
		if (tokenList == null || tokenList.isEmpty())
			return 0;
		else
			return tokenList.get(tokenList.size() - 1).getStopIndex() + 1;
	}

	/**
	 * Get list of all tokens
	 */
	public List<CommonToken> getTokenList() {
		return tokenList;
	}

	/**
	 * Set token list
	 */
	public void setTokenList(List<CommonToken> tokenList) {
		this.tokenList = tokenList;
	}

	/**
	 * Get dirty flag
	 */
	public boolean isDirty() {
		return dirty;
	}

	/**
	 * Set dirty flag
	 */
	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}
}
